﻿using ICAM.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ICAM.View
{
    /// <summary>
    /// VillageSelector.xaml 的交互逻辑
    /// </summary>
    public partial class VillageSelector : UserControl, INotifyPropertyChanged
    {
        
        public VillageSelector()
        {
            InitializeComponent();
            InitHousingEstates();
            this.DataContext = this;
        }


        private List<HousingEstateEx> _housingEstates = new List<HousingEstateEx>();
        public List<HousingEstateEx> HousingEstates
        {
            get { return this._housingEstates; }
            set {
                this._housingEstates = value;
                OnPropertyChanged("HousingEstates");
            }
        }

        private ObservableCollection<HousingEstateEx> _firstLevelItemSource = new ObservableCollection<HousingEstateEx>();
        public ObservableCollection<HousingEstateEx> FirstLevelItemSource
        {
            get { return this._firstLevelItemSource; }
            set {
                this._firstLevelItemSource = value;
                OnPropertyChanged("FirstLevelItemSource");
            }
        }

        private HousingEstateEx _firstLevelSelected = null;
        public HousingEstateEx FirstLevelSelected
        {
            get { return this._firstLevelSelected; }
            set {
                this._firstLevelSelected = value;
                OnPropertyChanged("FirstLevelSelected");

                if (this._firstLevelSelected != null)
                {
                    SecondLevelItemSource.Clear();
                    List<HousingEstateEx> second = _housingEstates.FindAll(a => a.IsHousingEstate == 1&&a.ParentHousingEstateID== this._firstLevelSelected.HousingEstateID);
                    if (second != null && second.Count > 0)
                    {
                        foreach (var per in second)
                        {
                            SecondLevelItemSource.Add(per);
                        }
                    }
                }
            }
        }

        private HousingEstateEx _secondLevelSelected = null;
        public HousingEstateEx SecondLevelSelected
        {
            get { return this._secondLevelSelected; }
            set {
                this._secondLevelSelected = value;
                OnPropertyChanged("SecondLevelSelected");
                if (this._secondLevelSelected != null)
                {
                    HousingEstateEx housingEstateEx = FirstLevelItemSource.SingleOrDefault(a => a.HousingEstateID == this._secondLevelSelected.ParentHousingEstateID);
                    if (housingEstateEx != null)
                    {
                        SelectedText = housingEstateEx.HousingEstateName+"/"+this._secondLevelSelected.HousingEstateName;
                        //DefaultTextVisibility = Visibility.Collapsed;
                        //SelectedTextVisibility = Visibility.Visible;
                    }
                }
            }
        }

        private ObservableCollection<HousingEstateEx> _secondLevelItemSource = new ObservableCollection<HousingEstateEx>();
        public ObservableCollection<HousingEstateEx> SecondLevelItemSource
        {
            get { return this._secondLevelItemSource; }
            set
            {
                this._secondLevelItemSource = value;
                OnPropertyChanged("SecondLevelItemSource");
            }
        }

        
        private string _selectedText = string.Empty;
        public string SelectedText
        {
            get { return this._selectedText; }
            set {
                this._selectedText = value;
                OnPropertyChanged("SelectedText");
            }
        }

        /// <summary>
        /// 初始化小区数据
        /// </summary>
        private void InitHousingEstates()
        {
            HousingEstateEx housingEstate1 = new HousingEstateEx();
            housingEstate1.HousingEstateID = 1;
            housingEstate1.IsHousingEstate = 0;
            housingEstate1.HousingEstateName = "东南区域";
            housingEstate1.HasItems = false;

            HousingEstateEx housingEstate2 = new HousingEstateEx();
            housingEstate2.HousingEstateID = 2;
            housingEstate2.IsHousingEstate = 0;
            housingEstate2.HousingEstateName = "西南区域";
            housingEstate2.HasItems = true;

            HousingEstateEx housingEstate3 = new HousingEstateEx();
            housingEstate3.HousingEstateID = 3;
            housingEstate3.IsHousingEstate = 0;
            housingEstate3.HousingEstateName = "西北区域";
            housingEstate3.HasItems = false;

            HousingEstateEx housingEstate4 = new HousingEstateEx();
            housingEstate4.HousingEstateID = 4;
            housingEstate4.ParentHousingEstateID = 2;
            housingEstate4.IsHousingEstate = 1;
            housingEstate4.HousingEstateName = "公园悦府";
            housingEstate4.HasItems = false;

            HousingEstateEx housingEstate5 = new HousingEstateEx();
            housingEstate5.HousingEstateID = 5;
            housingEstate5.ParentHousingEstateID = 2;
            housingEstate5.IsHousingEstate = 1;
            housingEstate5.HousingEstateName = "公馆1881";
            housingEstate5.HasItems = false;

            HousingEstateEx housingEstate6 = new HousingEstateEx();
            housingEstate6.HousingEstateID = 6;
            housingEstate6.ParentHousingEstateID = 2;
            housingEstate6.IsHousingEstate = 1;
            housingEstate6.HousingEstateName = "雍锦阁";
            housingEstate6.HasItems = false;

            _housingEstates.Add(housingEstate1);
            _housingEstates.Add(housingEstate2);
            _housingEstates.Add(housingEstate3);
            _housingEstates.Add(housingEstate4);
            _housingEstates.Add(housingEstate5);
            _housingEstates.Add(housingEstate6);

            List<HousingEstateEx> first = _housingEstates.FindAll(a => a.IsHousingEstate == 0);
            foreach (var per in first)
            {
                FirstLevelItemSource.Add(per);
            }
        }

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raises the PropertyChanged event.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        //private void btn_Click(object sender, RoutedEventArgs e)
        //{
        //    PART_Popup.IsOpen = true;
        //}

        //private void btnSource_LostFocus(object sender, RoutedEventArgs e)
        //{
        //    PART_Popup.IsOpen = false;
        //}
    }


    public class HousingEstateEx: HousingEstate
    {
        public bool HasItems { get; set; }
    }
}
